# coding=utf-8
import datetime
from django.contrib.auth import get_user_model, authenticate
import json
from django.http import HttpResponse
from django.contrib.auth import login as auth_login, get_user_model, authenticate

from blog.views import getResult, post_to_method_parmes


#



@post_to_method_parmes(['password','pseudonym', 'realname', 'manager_status'])
def reg_user(request, password, pseudonym, realname, manager_status):
    """
    注册用户
    by:李艳军 at:2017-07-19
    """
    user = get_user_model()()
    #密码加密
    user.username = realname

    user.set_password(password)
    #笔名
    user.name = pseudonym
    #真实名字
    user.realname = realname
    #注册时间
    now = datetime.datetime.now()
    user.create_time = now
    #管理员状态
    user.manager_status = manager_status
    user.save()

    return getResult(True, u'注册成功', user)

@post_to_method_parmes(['username', "password"])
def login(request, username, password):
    """
    登录
    by:闫宇 at:2016-05-16
    :param request:
    :return:
    """
    user = authenticate(username=username, password=password)
    if not user:
        return getResult(False, u'用户名密码错误', None)
    if not user.is_active:
        return getResult(False, u'用户已经停止使用。')
    auth_login(request, user)
    return getResult(True, u'登录成功', username)


def get_an_apple(request):

    return getResult(True, u'注册成功')